from PySide2.QtWidgets import QWidget, QPushButton, QLabel, QVBoxLayout, QComboBox, QHBoxLayout
from custom.UserWidget import UserWidget
from tool import dbutil
from manager import Manager
from custom.ConsoleWidget import ConsoleWidget

class AccountView(QWidget):
    def __init__(self):
        super(AccountView, self).__init__()
        self.selectIndex = -1

        self.setupUi()
        self.setupData()

    def setupUi(self):
        self.layout = QVBoxLayout(self)

        self.addBtn = QPushButton('添加用户')
        self.addBtn.clicked.connect(self.onClickAdd)

        self.actionLayout = QHBoxLayout()
        self.platformLabel = QLabel("平台")
        self.editButton = QPushButton("编辑")
        self.editButton.hide()
        self.editButton.clicked.connect(self.onClickEdit)
        self.actionLayout.addWidget(self.platformLabel)
        self.actionLayout.addWidget(self.editButton)
        self.actionLayout.addStretch(1)

        self.combBox = QComboBox()
        self.combBox.currentIndexChanged.connect(self.onChangeAccount)

        self.consoleWidget = ConsoleWidget()
        Manager.consoleWidget = self.consoleWidget

        self.layout.addWidget(self.addBtn)
        self.layout.addLayout(self.actionLayout)
        self.layout.addWidget(self.combBox)
        # self.layout.addStretch(1)
        self.layout.addWidget(self.consoleWidget)

    def setupData(self):
        self.platforms = dbutil.fetchPlatform(2)
        self.uploaders = dbutil.fetchUploaders()

        accountList = []
        selectIndex = 0
        for index in range(len(self.uploaders)):
            uploader = self.uploaders[index]
            accountList.append(uploader.account)
            if uploader.selected == 1:
                selectIndex = index
        self.combBox.clear()
        self.combBox.addItems(accountList)
        self.combBox.setCurrentIndex(selectIndex)

        if len(accountList) > 0:
            self.editButton.show()

    def setCurrentUploader(self, index):
        self.currentUploador = self.uploaders[index]
        platform = "{}({}) 已传({})".format(
            self.currentUploador.platform_name, self.currentUploador.number, self.currentUploador.uploaded_num)
        self.platformLabel.setText(platform)
        Manager.currentUploador = self.currentUploador
        if self.selectIndex == index or index < 0:
            return
        if self.selectIndex >= 0:
            dbutil.updateUploader(self.uploaders[self.selectIndex].id, {"selected": "0"})
        dbutil.updateUploader(self.currentUploador.id, {"selected": "1"})
        self.selectIndex = index

    def onChangeAccount(self, i):
        self.setCurrentUploader(i)

    def onClickAdd(self):
        self.showUserwidget("添加用户", None)

    def onClickEdit(self):
        self.showUserwidget("编辑用户", self.currentUploador)


    def showUserwidget(self, title, data):
        UserWidget(title, data, self.userWidgetCallback)
    
    def userWidgetCallback(self, name, pwd, platform, number, id):
        dbutil.insertUploader(name, pwd, platform["id"], number, id)
        self.setupData()

    def onUpdateUploador(self):
        pass
